//
//  NewFeatureController.m
//  微博2期
//
//  Created by J on 15/11/23.
//
//

#import "NewFeatureController.h"
#import "TabbarViewController.h"
#define count 4
@interface NewFeatureController()<UIScrollViewDelegate>
{
    UIPageControl * _pageControl;
}

@property(nonatomic,strong) UIScrollView *scrollView;
@end
@implementation NewFeatureController
- (void)viewDidLoad{
    [super viewDidLoad];
    

    
    //1.创建一个ScrollView 显示所有的新特性图片
    
    UIScrollView *scrollView =[[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    CGFloat scrollViewW = scrollView.width;
    CGFloat scrollViewH = scrollView.height;
    //2.添加图片到scrollView中

    for (int i=0; i<count; i++) {
        
        UIImageView * imageView = [[UIImageView alloc]init];
        imageView.size = scrollView.size;
        imageView.x = scrollViewW*i;
      
        NSString * name =[NSString stringWithFormat:@"new_feature_%d",i+1];  //显示图片
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
       
        if (i == count - 1) { //如果是最后一个imageView 就往里面添加其他内容
            [self setupLastImageView:imageView];
        }
        
        
    }
    
    //默认情况下 scrollView 一旦创建出来里面就存在一些子控件 就算不主动添加子控件 它内部还是可能会有一些子控件
    //3.设置scrollView其他属性
    
    scrollView.contentSize = CGSizeMake(count*scrollViewW, 0);//如果想让某个方向不能滚动 那么这个方向对应尺寸数值0
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    
    
    //4.添加pageControl分页 展示目前看的是第几页
    UIPageControl * pageControl = [[UIPageControl alloc]init];
    [self.view addSubview:pageControl];//添加到控制器的view
//    pageControl.width = 100;
//    pageControl.height = 100;//一个控件就算它尺寸为0 它里面的子控件依然可以出现
//    pageControl.userInteractionEnabled = NO;//让点击小圆点的时候无效   另一种方式 是不设置高度  如果不设置宽高 效果等同于这句

    pageControl.centerX = scrollViewW * 0.5;
    pageControl.centerY = scrollViewH -50;
    pageControl.numberOfPages = count;
    pageControl.currentPageIndicatorTintColor = Color(253, 98, 42);
    pageControl.pageIndicatorTintColor = Color(189, 189, 189);
    _pageControl =pageControl;
    pageControl.backgroundColor = [UIColor orangeColor];//如果不设置高度 仍然显示 但是 设置背景色无效
}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView*)imageView{
    //1.分享给大家(checkbox)
    imageView.userInteractionEnabled = YES;
    UIButton * shareButton = [[UIButton alloc]init];
    [shareButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareButton setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [shareButton addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    shareButton.width = 200;
    shareButton.height = 30;
    [imageView addSubview:shareButton];
    shareButton.centerX = imageView.width * 0.5;
    shareButton.centerY = imageView.height * 0.65;
//    shareButton.backgroundColor = [UIColor orangeColor];
//    shareButton.titleLabel.backgroundColor = [UIColor purpleColor];
//    会影响按钮内部所有内容(里面imageView和titleLabel)
//    shareButton.contentEdgeInsets = UIEdgeInsetsMake(10, 100, 0, 100);
//   只影响按钮内部的titleLabel
    shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
//     只影响按钮内部的imageView
    shareButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);

    [imageView addSubview:shareButton];//    shareButton.alpha = 0.01; //<=0.01将会看不见

    //2. 开始微博
    UIButton * startBtn = [[UIButton alloc]init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = imageView.width * 0.5;
//    NSLog(@"%f",shareButton.centerX);
    startBtn.centerY = imageView.height*0.75;
//    startBtn.frame = CGRectMake(0, 0, 100, 100);
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];

    [imageView addSubview:startBtn];
//    NSLog(@"%f",startBtn.size.height);
    //会影响按钮内部所有内容

    
    
//    UITextField * text =[[UITextField alloc]init];
//    text.borderStyle =UITextBorderStyleRoundedRect;//不设置边框或者文字会显示但看不见
//    text.frame = CGRectMake(10, 20, 100, 100);
//    [self.view addSubview:text];
//    
    
    
}


//新特性按钮
- (void)startClick{
    //切换到tabbarController
    
    /*
     切换控制器手段
     1.push 依赖UINavigationController , 控制器的切换是可逆的, A切换到B ,B又可以切换到A
     2.modal 控制器的切换是可逆的, A切换到B ,B又可以切换到A
     3.切换window 的rootViewController  不可逆
     */
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[TabbarViewController alloc]init];//rootViewController就不再指向 新特性 控制器 然后调用 dealloc
    
//    [self presentViewController:[[TabbarViewController alloc]init] animated:YES completion:nil];//这样的话新特性控制器不会被销毁 rootViewController 一直强引用它
    
}
- (void)dealloc{
    
//    NSLog(@"%s",__FUNCTION__);
}
- (void)shareClick:(UIButton*)sender{
    //状态取反
    sender.selected =! sender.selected;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"%@",self.scrollView.subviews);

    double page =scrollView.contentOffset.x / scrollView.width;
    _pageControl.currentPage = (int)(page + 0.5);//四舍五入计算页码 只适合一位小数
    

}

/*
 一个控件用肉眼看不见, 有哪些可能
 1.没有实例化控件
 2.没有设置尺寸
 3.控件的颜色可能和父控件背景色一样(实际上已经显示了,只不过用肉眼看不见了)
 4.透明度alpha <= 0.01
 5.hidden = YES
 6.没有添加到父控件中
 7.被其他控件挡住了
 8.位置不对
 9.父控件发生了以上情况
 特殊情况:
 UIImageView 没设置图片 或者设置的图片名不对
 UILabel 没有设置文字或者文字颜色和父控件背景色一样 
 UIPageControl 没有设置总页数, 不会显示小圆点
 UIButton 内部imageView和titleLabel被篡改了 ,或者 imageView和titleLabel没有内容
 UITextField 没有设置borderStyle样式 或者 没有设置文字
 
 添加一个控件的建议(调试技巧):
 1.最好设置背景色和尺寸
 2.控件的颜色尽量不要跟父控件的背景色一样
 */
@end
